package by.grsu.pereverzev.page;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.pereverzev.app.PereverzevWebApplication;
import by.grsu.pereverzev.dao.impl.BillDao;
import by.grsu.pereverzev.entity.Bill;
import by.grsu.pereverzev.entity.Passenger;

public class BillListPage extends WebPage{
	
	private final BillDao billDao = new BillDao(PereverzevWebApplication.DATA_XML_FOLDER);
	
	public BillListPage(final Passenger passenger) {
		super();
		List<Bill> bills = billDao.getByPassengerId(passenger.getId());
		
		add(new DataView<Bill>("table-rows", new ListDataProvider<Bill>(bills)) {
			@Override
			protected void populateItem(final Item<Bill> item) {
				final Bill bill = item.getModelObject();
				
				item.add(new Label("id", bill.getId()));			
				item.add(new Label("passengerName", passenger.getName()));
				item.add(new Label("cost", bill.getValue()));
				item.add(new Label("isPayed", bill.isPayed()));
					
				item.add(new Link("payLink") {
					@Override
					public void onClick() {
						bill.setPayed(true);
						billDao.update(bill);
						setResponsePage(new BillListPage(passenger));
					}
				});
				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						billDao.delete(bill.getId());
						setResponsePage(new BillListPage(passenger));
					}
				});
			}
		});
		
	}

}
